/**
 * form 操作方法，依赖jquery jquery-validate
 */
"use strict";

var form = {
	/**
	 * 取值，赋值
	 */
	val(elem, value){
		var $elem = null;
		if(elem instanceof jQuery){
			$elem = elem;
		}else{
			$elem = $(elem);
		}
		if(value){
			for(var key in value){
				var $item = $elem.find(`[name='${key}']`)
				if($item.length == 0){
					continue;
				}
				if($item.prop("nodeName").toUpperCase() == "TEXTAREA "){
					$item.text(value[key]);
				}else{
					$item.val(value[key]);
				}
			}
		}else{
			var obj = {}
			$elem.serializeArray().forEach(item=>{
				obj[item.name] = item.value;
			});
			return obj;
		}
	},
	
	/**
	 * 校验
	 */
	validate(elem){
		var $elem = null;
		if(elem instanceof jQuery){
			$elem = elem;
		}else{
			$elem = $(elem);
		}
		return $elem.valid();
	},
}

export default form;

